package com.hong.collections.custom;

/**
 * @Auther: Administrator
 * @Date: 2018/7/31 21:11
 * @Description:模范list
 */
public class LinkedList<E> {
    /**
     * 对象
     */
    private Node<E> first;
    /**大小*/
    private int size;
    /**空对象*/
    private final Node<E> NULL  =(Node<E>)null;
    /**返回空*/
    private final static String PLAIN_NULL="null";

    public LinkedList() {
        this.first = NULL;
    }

    public int size(){
        return size;
    }

    public static <E> LinkedList<E> of(E ...elements) {
        final LinkedList list = new LinkedList<>();
        if (elements.length != 0) {
            for (E ele : elements) {
                list.addFirst(ele);
            }
        }
        return list;
    }

    public boolean contains(E e){
        Node<E> current = first;
        while (current!=null){
            if(current.value==e){
                return true;
            }
            current = current.next;
        }
        return false;
    }

    /**
     * 异常
     */
    static class NoElementException extends RuntimeException{
        public NoElementException(String message){
            super(message);
        }
    }

    public E removeFirst(){
        /**
         * 如果为空则报异常
         */
        if(this.isEmpty()){
            throw new NoElementException("The linked list is empty.");
        }
        Node<E> node = first;
        first = node.next;
        size--;
        return node.value;
    }

    public LinkedList<E> addFirst(E e){
        final Node<E> newNode = new Node<>(e);
        newNode.next = first;
        this.size++;
        this.first = newNode;
        return this;
    }

    public boolean isEmpty(){
        return (size() ==0 );
    }

    private static class Node<E>{
        E value;
        Node<E> next;
        public Node(E value){
            this.value = value;
        }

        @Override
        public String toString() {
            if(null!=value){
                return  value.toString();
            }
            return PLAIN_NULL;
        }
    }

    @Override
    public String toString() {
        if(this.isEmpty()){
            return "[]";
        }else{
            StringBuilder builder = new StringBuilder("[");
            Node<E> node= first;
            while (node!=null){
                builder.append(node.toString()).append(",");
                node = node.next;
            }
            builder.replace(builder.length()-1,builder.length(),"]");
            return  builder.toString();
        }

    }

    public static void main(String[] args) {
        LinkedList<String> list = of("Hello", "World", "Scala", "Java", "Thread");
        list.addFirst("Concurrency").addFirst("Test");
        System.out.println(list.size);
        System.out.println(list.contains("Scala"));
        System.out.println("========================");
        System.out.println(list);
        while (!list.isEmpty()){
            System.out.println(list.removeFirst());
        }
        System.out.println(list.size());
        System.out.println(list.isEmpty());

    }
}
